import Vue from "vue"
import Vuex from "vuex"
import https from "../axiosurl"
// 手动导入测点数据
 
Vue.use(Vuex);
 
export default new Vuex.Store({
    state:{
      adata: ["今天", "昨天"],
      // 所有数据
      AllData:[],
      // 应变
      stress1:[],
      // 频率
      frequency:[],
      // 环境信息
      temdata:0,
      humidity:0,
      // 转体数据集合
      // 基本信息
      sensorgroup:[],
      // data最新数据
      newdata:[],
      // sensorvalue数据
      SVdata:[],
      // 转体实时角度(需要限制正数为小数点后一位，负数包括符号到小数点后一位)
      cornor:0,
      // 倾角数据
      dipanglex:[],
      dipangley:[],
      // 加速度
      speed1:[],
      // 角速度
      speed2:[],
      // 偏轴距离
      displacement:0,
      // 挠度
      deflection:[],
      // 应力
      stress2:[],
      // 速率
      speedrate:0,
      // 转角数组
      cornorarr:[],
      // 当前时间
      timenow:0,
      //支腿反力平均值
      average:[],
      // 上次数据存档
      oldarr:[],
      textold:[],
      legarr1:[],
      legarr2:[],
    },

    getters:{

    },
    mutations:{
      // 全部数据处理
      // 时间戳处理为标准时间(到时候放进别的函数里面)
      timeChange(time){
        let datetime = time - 404467200000;
        let date = new Date(datetime);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        let D = date.getDate() + ' ';
        let h = date.getHours() + ':';
        let m = date.getMinutes() < 10 ? '0'+date.getMinutes() + ':' : date.getMinutes() + ':';
        let s = date.getSeconds();
        return Y+M+D+h+m+s
      },
      // 获取转速(有问题)
      getspeedrate(state){
        // var newcornor = state.SVdata.filter((element) => {
        //   return element.sid == 32
        // })
        if(state.oldarr.length){
          let time1 = state.cornorarr[0].time - state.oldarr[0].time
          var chazhi = Number(state.cornorarr[0].value) - Number(state.oldarr[0].value)
          if(chazhi < 0){
            chazhi = 0 - chazhi
          }
          console.log('测试1',time1);
          console.log('测试2',chazhi);
          if (time1 < 100){
            state.speedrate = (state.speedrate/2).toFixed(2)
          }else{
            state.speedrate = ((chazhi * 60000)/time1).toFixed(2)
          }
          // console.log('转速',state.speedrate);
        }else{
          return;
        }
      },
      // 从sensorvalue中提取数据并分组并处理
      getDataFromSV(state){
        // 更新时间转换,小数点限制.
        var i = 0;
        for( i = 0 ;i< state.SVdata.length;i++){
          let time = state.SVdata[i].updateTime - 404467165000;
          state.SVdata[i].time = time
          let timenow = new Date()
          state.timenow = timenow
          
          let date = new Date(time);
          let h = date.getHours() < 10 ? '0'+date.getHours() + ':' : date.  getHours() + ':';
          let m = date.getMinutes() < 10 ? '0'+date.getMinutes() + ':' : date.  getMinutes() + ':';
          let s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.  getSeconds();
          state.SVdata[i].updateTime = h+m+s
          state.SVdata[i].value = Number(state.SVdata[i].value).toFixed(3)
          state.SVdata[i].timecha = timenow - time
        }
        // 健康判断
        var maxtime = 0
        for(let j = 0;j < state.SVdata.length;j++){
          maxtime = state.SVdata[j].time > maxtime ? state.SVdata[j].time : maxtime
        }
        // console.log('最大时间',maxtime);
        for(let j = 0;j < state.SVdata.length;j++){
          let timetype = maxtime - state.SVdata[j].time
          if (timetype <= 60000){
            state.SVdata[j].timetype = '健康'
          }else if (timetype <= 3600000){
            state.SVdata[j].timetype = '良好'
          }else{
            state.SVdata[j].timetype = '故障'
          }
        }
        // 子组件位置添加
        for(let i=0 ; i < state.SVdata.length;i++){
          switch(state.SVdata[i].sid ){
            case 7 :     
            case 10:
            case 13:
              state.SVdata[i].place = '长臂-砼'
              break;
            case 8 : 
            case 11 :
            case 14 :
              state.SVdata[i].place = '短臂-砼'
              break;
            case 6:
            case 9:
            case 12:
              state.SVdata[i].place = '梁根部'
              break;
            case 15: case 16 :case 17 :case 18 :case 19 :case 20 :case 21 :case 22 :case 23 :case 24 :case 25 :case 26:
              state.SVdata[i].place = '支腿'
              break;
            case 27 :case 33 :case 41:
              state.SVdata[i].place = '长臂端'
              break;
            case 46:
              state.SVdata[i].place = '长臂-z'
              break;
            case 47:
              state.SVdata[i].place = '长臂-x'
              break;
            case 28 :case 34:
              state.SVdata[i].place = '长臂中点'
              break;
            case 29 :case 35 :case 40:
              state.SVdata[i].place = '转动中心'
              break;
            case 30 :case 36:
              state.SVdata[i].place = '短臂中点'
              break;
            case 31 :case 37 :case 39 :case 45:
              state.SVdata[i].place = '短臂端'
              break;
            case 43 :
              state.SVdata[i].place = '短臂-z'
            break;
            case 44 :
              state.SVdata[i].place = '短臂-x'
            break;
            case 32 :case 38:
              state.SVdata[i].place = '主塔'
              break;
            case 42 :
              state.SVdata[i].place = '球铰'
              break;
            default:
              break;
          }
          // if(state.SVdata[i].sid == 17 || state.SVdata[i].sid == 18){  
          //   state.SVdata[i].place = '短臂端'
          // }else if (state.SVdata[i].sid == 16 || state.SVdata[i].sid == 32){
          //   state.SVdata[i].place = '球铰'
          // }else if (state.SVdata[i].sid == 1 || state.SVdata[i].sid == 2 || state.SVdata[i].sid == 3 || state.SVdata[i].sid == 4){
          //   state.SVdata[i].place = '梁根部'
          // }else{
          //   state.SVdata[i].place = '长臂端'
          // }
        }
        console.log('处理后数据',state.SVdata);
        // 子组件所需数据长度扩展
        function lenghtChange(arr){
          // 先防止arr为空
          if(arr.length == 0){
            return 
          }
          if(arr.length <=10){
            var i = arr.length - 1;
            var k = arr.length
            while(i<=10){
              let j = 0;
              for(j=0;j<k;j++){
                arr.push(arr[j])
                i++
              }
            }
            return arr
          }else{
            return arr
          }
        }
        // 转角获取
        state.cornorarr = state.SVdata.filter((element) => {
          return element.sid == 42
        })
        if(state.cornorarr.length){
          state.cornor = Number(state.cornorarr[0].value).toFixed(1)
          // console.log('转角数据',state.cornorarr);
        }else{
          console.log('转角数据请求有误');
        }
        // 应力获取
        state.stress1 = state.SVdata.filter((element) => {          
          return element.sid == 6 || element.sid == 7 || element.sid == 8 || element.sid == 9 || element.sid == 10 || element.sid == 11 
        })
        lenghtChange(state.stress1)
        state.stress2 = state.SVdata.filter((element) => {          
          return  element.sid == 12 || element.sid == 13 || element.sid == 14
        })
        lenghtChange(state.stress2)
        // 频率获取
        // state.frequency = state.SVdata.filter((element) => {          
        //   return element.sid == 2 || element.sid == 4 
        // })
        // lenghtChange(state.frequency)
        // console.log('应变',state.frequency);
        // 温度湿度获取
        let temarr = state.SVdata.filter((element) => {          
          return element.sid == 5
        })
        state.temdata = Number(temarr[0].value).toFixed(1)
        // console.log('温度',state.temdata);
        let humarr = state.SVdata.filter((element) => {          
          return element.sid == 4
        })
        state.humidity = Number(humarr[0].value).toFixed(1)
        // console.log('湿度',state.humidity);
        // 支腿
        state.legarr1 = state.SVdata.filter((element) => {          
          return element.sid == 15 || element.sid ==17 || element.sid == 19 || element.sid == 21  || element.sid == 23 || element.sid == 25
        })
        lenghtChange(state.legarr1)
        state.legarr2 = state.SVdata.filter((element) => {          
          return element.sid == 16 || element.sid ==18 || element.sid == 20 || element.sid == 23  || element.sid == 24 || element.sid == 26
        })
        lenghtChange(state.legarr2)
        // 倾角-x   
        state.dipanglex = state.SVdata.filter((element) => {          
          return element.sid == 27 || element.sid ==28 || element.sid == 29 || element.sid == 30  || element.sid == 31 || element.sid == 32
        })
        lenghtChange(state.dipanglex)
        // console.log('倾角-x：',state.dipanglex);
        state.dipangley = state.SVdata.filter((element) => {          
          return element.sid == 33 || element.sid == 34 || element.sid == 35 || element.sid == 36 || element.sid == 37 || element.sid == 38 
        })
        lenghtChange(state.dipangley)
        // console.log('倾角-y：',state.dipangley);
        // 加速度
        state.speed1 = state.SVdata.filter((element) => {          
          return element.sid == 43 || element.sid == 44 || element.sid == 46 || element.sid == 47
        })
        lenghtChange(state.speed1)
        // console.log('加速度：',state.speed1);
        // 角速度
        // state.speed2 = state.SVdata.filter((element) => {          
        //   return element.sid == 43 || element.sid == 44
        // })
        // lenghtChange(state.speed2)
        // console.log('角速度：',state.speed2);
        // 偏轴距离----------------------------目前没有数据
        let displacementarr = state.SVdata.filter((element) => {          
          return element.sid == 45
        })
        state.displacement = Number(displacementarr[0].value).toFixed(1)
        // console.log('位移：',state.displacement);
        // 挠度
        state.deflection = state.SVdata.filter((element) => {          
          return element.sid == 39 || element.sid == 40 || element.sid == 41
        })
        lenghtChange(state.deflection)
        // console.log('挠度：',state.deflection);
        // 应力
        // lenghtChange(state.stress)
        
      },
      // 取平均值
      getaverage(state){
        var average = []
        for(let i = 0; i< 6;i++){
          let str1 = {}
          str1.value = ((Number(state.legarr1[i].value) + Number(state.legarr2[i].value))/2).toFixed(3)
          str1.sid = state.legarr1[i].sid +"+"+ state.legarr2[i].sid
          str1.place = '支腿' + (i + 1)
          if(i > 0){
            str1.place = '支腿' + (7 - i)
          }
          average.push(str1)
        }
        state.average = average
        // console.log('支腿反力',state.average);
        // 子组件所需数据长度扩展
        function lenghtChange(arr){
          // 先防止arr为空
          if(arr.length == 0){
            return 
          }
          if(arr.length <=10){
            var i = arr.length - 1;
            var k = arr.length
            while(i<=10){
              let j = 0;
              for(j=0;j<k;j++){
                arr.push(arr[j])
                i++
              }
            }
            return arr
          }else{
            return arr
          }
        }
        lenghtChange(state.average)
      }
    },

    actions:{
      // context代表当前store实例
      // 请求基本信息
      getBasicData(context){
        return new Promise((resolve) => {
          https.fetchGet('/getSensor').then(
            res => {
              context.state.sensorgroup = res.data.data;
              console.log('基本信息获取成功',context.state.sensorgroup);
              resolve('第一步完成')
            }).catch(err => {
              alert('基本信息请求错误')
              console.log(err);
            })
        })
      },
      // 请求总数据条数100(数量对象为未编辑)
      // 同时获取最新cornor
      getAllData(context,num){
        return new Promise((resolve) => {
          https.fetchGet('/getDataByNumber',{number:num}).then(
            res => {
              context.state.newdata = res.data.data
              // 提取最新cornor
              console.log('全部数据最新100条获取成功',context.state.newdata);
              resolve('第二步完成')
            }).catch(err => {
              alert('全部数据请求错误')
              console.log(err);
            })
        })
      },
      // 请求dataaction？？？？（数量暂定200）？？？？？？？？？？？？？
      getActionData(context){
        return new Promise((resolve) => {
          https.fetchGet('/getDataActionByNumber',{number:200}).then(
            res => {
              console.log('dataaction获取成功',res);
              resolve('第?步完成')
            }).catch(err => {
              alert('dataaction请求错误')
              console.log(err);
            })
        })
      },
      // 请求display数据??????？？？？？？？？？？？？？？？？？？
      getDisplayData(context){
        return new Promise((resolve) => {
          https.fetchGet('/getDisplayByNumber',{number:50}).then(
            res => {
              console.log('Display获取成功',res);
              resolve('第?步完成')
            }).catch(err => {
              alert('Display请求错误')
              console.log(err);
            })
        })
      },
      // 请求传感器总体值,
      // 并更新挠度和子组件们的数据,
      // 数据们进行处理
      getSensorValueData(context){
        return new Promise((resolve) => {
        https.fetchGet('/getSensorValueByNumber',{number:50}).then(
          res => {
            // console.log('旧数据',context.state.textold);
            context.state.SVdata = res.data.data
            console.log('传感器值获取成功',context.state.SVdata);
            context.commit('getDataFromSV')
            context.commit('getspeedrate')
            context.commit('getaverage')
            // 转角旧数据数据慢一步存入
            context.state.oldarr = context.state.SVdata.filter((element) => {
              return element.sid == 42
            })
            // context.state.textold = context.state.SVdata
            resolve('第二步完成')
          }).catch(err => {
            alert('传感器值请求错误')
            console.log(err);
          })
        })
      },
    }
})